package com.scedu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.scedu.springcloud.handler.CustomerBlockHandler;
import com.scedu.springcloud.springcloud.entity.CommonResult;
import com.scedu.springcloud.springcloud.entity.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {
    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl",
            blockHandlerClass= CustomerBlockHandler.class, //自定义限流处理类
            blockHandler = "handlerException1")  //自定义限流处理类中的方法
    public CommonResult testbyUrl(){

        return new CommonResult(200,"按Url测试ok",new Payment(2020l,"SERAIL02"));

    }
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handler_byResource")
    public CommonResult testbyResource(){

        return new CommonResult(200,"按资源名测试ok",new Payment(2020l,"SERAIL01"));

    }
    //限流时返回的方法
    public CommonResult handler_byResource
    (
     BlockException exception){

        return new CommonResult(200,"服务不可用"+exception.getClass().getCanonicalName());
    }
}
